package com.yanqu.road.server.manager.strongholdwar;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.stronghold.CrossActivityUnionRankModel;
import com.yanqu.road.logic.cross.stronghold.CrossStrongholdActivityRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdGroupRankModel {
    private int activityId;
    private int groupId;
    //conditionType
    private Map<Integer, CrossStrongholdActivityRankListModel> crossUserRankModelMap = new ConcurrentHashMap<>();
    //conditionType
    private Map<Integer, CrossActivityUnionRankModel> crossUnionRankModelMap = new ConcurrentHashMap<>();

    public CrossStrongholdGroupRankModel(int activityId, int groupId) {
        this.activityId = activityId;
        this.groupId = groupId;
    }

    public CrossStrongholdActivityRankListModel getUserRankModel(int conditionType, boolean newIfNull) {
        CrossStrongholdActivityRankListModel model = crossUserRankModelMap.get(conditionType);
        if (null == model && newIfNull) {
            synchronized (crossUserRankModelMap) {
                model = crossUserRankModelMap.get(conditionType);
                if (null == model) {
                    model = new CrossStrongholdActivityRankListModel(activityId, groupId, conditionType, true);
                    int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossStrongholdActivityMgr.getActivityConditionInfoList(activityId));
                    if (maxRank <= 0) {
                        maxRank = 500;
                    }
                    model.setRankNum(maxRank);
                    crossUserRankModelMap.put(conditionType, model);
                }
            }
        }
        if (model != null) {
            int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossStrongholdActivityMgr.getActivityConditionInfoList(activityId));
            if (maxRank <= 0) {
                maxRank = 500;
            }
            model.setRankNum(maxRank);
        }
        return model;
    }

    public List<CrossStrongholdActivityRankListModel> getUserRankModelList() {
        List<CrossStrongholdActivityRankListModel> modelList = new ArrayList<>();
        modelList.addAll(crossUserRankModelMap.values());
        return modelList;
    }

    public CrossActivityUnionRankModel getUnionRankModel(int conditionType, boolean newIfNull) {
        CrossActivityUnionRankModel model = crossUnionRankModelMap.get(conditionType);
        if (null == model && newIfNull) {
            synchronized (crossUnionRankModelMap) {
                model = crossUnionRankModelMap.get(conditionType);
                if (null == model) {
                    model = new CrossActivityUnionRankModel(activityId, groupId, conditionType);
                    int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossStrongholdActivityMgr.getActivityConditionInfoList(activityId));
                    if (maxRank <= 0) {
                        maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                    }
                    model.setRankNum(maxRank);
                    crossUnionRankModelMap.put(conditionType, model);
                }
            }
        }
        return model;
    }

    public List<CrossActivityUnionRankModel> getUnionRankModelList() {
        List<CrossActivityUnionRankModel> modelList = new ArrayList<>();
        modelList.addAll(crossUnionRankModelMap.values());
        return modelList;
    }

    public void save(){
        for (CrossStrongholdActivityRankListModel model : crossUserRankModelMap.values()) {
            model.saveNewCrossActivityRankList();
        }
        for (CrossActivityUnionRankModel model : crossUnionRankModelMap.values()) {
            model.save();
        }
    }
}
